package com.john.myokhttpimagerecycledemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.john.myokhttpimagerecycledemo.R;
import com.john.myokhttpimagerecycledemo.bean.IBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * author: John
 * time: 2016/7/13
 * overview:
 * usage:
 */
public class MAdapter extends RecyclerView.Adapter<MAdapter.MHolder> {


    public List<IBean.ListBean> mDatas;
    private Context mContext;
    public DisplayImageOptions mOptions;

    public MAdapter(Context context, List<IBean.ListBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        mOptions=new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnLoading(R.mipmap.timg)
                .build();
    }

    @Override
    public MHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_layout_style, parent, false);
        return new MHolder(view);
    }

    @Override
    public void onBindViewHolder(MHolder holder, int position) {
        IBean.ListBean bean = mDatas.get(position);
        holder.tv.setText(bean.goods.desc);
        ImageLoader.getInstance().displayImage(bean.goods.gpic.get(0), holder.imageView,mOptions);
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }


    public class MHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        private ImageView imageView;

        public MHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.iv_item);
            tv = (TextView) itemView.findViewById(R.id.tv_item);
        }
    }

}
